package com.mlethe.library.multi.api;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.alipay.share.sdk.openapi.APImageObject;
import com.alipay.share.sdk.openapi.APMediaMessage;
import com.alipay.share.sdk.openapi.APTextObject;
import com.alipay.share.sdk.openapi.APWebPageObject;
import com.alipay.share.sdk.openapi.IAPApi;
import com.alipay.share.sdk.openapi.SendMessageToZFB;
import com.mlethe.library.multi.api.entity.ImageObject;
import com.mlethe.library.multi.api.entity.ShareImage;
import com.mlethe.library.multi.api.entity.ShareMini;
import com.mlethe.library.multi.api.entity.ShareMusic;
import com.mlethe.library.multi.api.entity.ShareText;
import com.mlethe.library.multi.api.entity.ShareVideo;
import com.mlethe.library.multi.api.entity.ShareWeb;
import com.mlethe.library.multi.api.listener.Callback;
import com.mlethe.library.multi.api.listener.OnActionCallback;
import com.mlethe.library.multi.api.listener.OnActionListener;
import com.mlethe.library.multi.api.listener.OnCompressListener;
import com.sina.weibo.sdk.api.MediaObject;
import com.sina.weibo.sdk.api.MultiImageObject;
import com.sina.weibo.sdk.api.SuperGroupObject;
import com.sina.weibo.sdk.api.TextObject;
import com.sina.weibo.sdk.api.VideoSourceObject;
import com.sina.weibo.sdk.api.WebpageObject;
import com.sina.weibo.sdk.api.WeiboMultiMessage;
import com.sina.weibo.sdk.openapi.IWBAPI;
import com.tencent.connect.common.Constants;
import com.tencent.connect.share.QQShare;
import com.tencent.connect.share.QzonePublish;
import com.tencent.connect.share.QzoneShare;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXMiniProgramObject;
import com.tencent.mm.opensdk.modelmsg.WXMusicVideoObject;
import com.tencent.mm.opensdk.modelmsg.WXTextObject;
import com.tencent.mm.opensdk.modelmsg.WXVideoObject;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分享
 *
 * @author Mlethe
 * @date 2024/12/22
 */
public final class ShareAction {

    /**
     * 分享方式
     */
    private final MultiMedia mMultiMedia;

    /**
     * 回调监听
     */
    private OnActionListener mListener;

    /**
     * 图片压缩监听
     */
    private OnCompressListener mCompressListener;

    /**
     * 分享内容
     */
    private Share mShare;

    /**
     * 分享场景
     */
    private ShareScene mScene;

    public ShareAction(MultiMedia media) {
        this.mMultiMedia = media;
    }

    /**
     * 分享回调
     *
     * @param listener
     */
    public ShareAction setCallback(OnActionListener listener) {
        this.mListener = listener;
        return this;
    }

    /**
     * 图片压缩监听回调
     *
     * @param listener
     */
    public ShareAction setCompressListener(OnCompressListener listener) {
        this.mCompressListener = listener;
        return this;
    }

    /**
     * 设置分享类型
     *
     * @param scene
     */
    public ShareAction setShareScene(ShareScene scene) {
        this.mScene = scene;
        return this;
    }

    /**
     * 分享内容
     *
     * @param share
     */
    public ShareAction setShare(Share share) {
        this.mShare = share;
        return this;
    }

    /**
     * 分享
     *
     * @param activity
     */
    public void share(Activity activity) {
        OnActionListener listener = mListener;
        Share share = mShare;
        mListener = null;
        mShare = null;
        MultiApi instance = MultiApi.getInstance();
        Context context = instance.getContext();
        if (context == null) {
            onFailure(instance, listener, share, -8000, "MultiApi未初始化");
            return;
        }
        if (share == null) {
            onFailure(instance, listener, share, -8003, context.getString(R.string.multi_api_parameter_error));
            return;
        }
        boolean status = instance.postAsync(() -> {
            try {
                if (share instanceof ShareText) {
                    if (MultiMedia.WECHAT == mMultiMedia) {
                        IWXAPI api = getWXApi(instance, share, listener);
                        if (api == null) {
                            return;
                        }
                        ShareText shareData = (ShareText) share;
                        if (!shareData.isValid()) {
                            onFailure(instance, listener, share, -8003, context.getString(R.string.multi_api_parameter_error));
                            return;
                        }
                        if (mScene == ShareScene.FAVORITE) {
                            // 文字分享到收藏时，没有描述无法弹出分享页面
                            shareData.setDescription(shareData.getText());
                        }
                        WXTextObject wxObj = new WXTextObject();
                        wxObj.text = shareData.getText();
                        wxSendReq(context, instance, api, wxObj, share, listener);
                    } else if (MultiMedia.QQ == mMultiMedia) {
                        Tencent tencent = getTencent(instance, share, listener);
                        if (tencent == null) {
                            return;
                        }
                        ShareText shareData = (ShareText) share;
                        if (!shareData.isValid()) {
                            onFailure(instance, listener, share, -8003, context.getString(R.string.multi_api_parameter_error));
                            return;
                        }
                        shareData.setTitle(shareData.getText());
                        Bundle params = new Bundle();
                        if (mScene == ShareScene.FRIEND) {
                            params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
                            params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, shareData.getTargetUrl());
                            qqSendReq(instance, tencent, activity, params, share, listener, 1);
                        } else if (mScene == ShareScene.CIRCLE) {
                            params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT);
                            params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, shareData.getTargetUrl());
                            qqSendReq(instance, tencent, activity, params, share, listener, 2);
                        } else {
                            onFailure(instance, listener, null, -9500, context.getString(R.string.multi_api_sharing_type_error));
                        }
                    } else if (MultiMedia.ALIPAY == mMultiMedia) {
                        IAPApi api = getAPApi(instance, share, listener);
                        if (api == null) {
                            return;
                        }
                        ShareText shareData = (ShareText) share;
                        if (!shareData.isValid()) {
                            onFailure(instance, listener, share, -8003, context.getString(R.string.multi_api_parameter_error));
                            return;
                        }
                        APTextObject textObject = new APTextObject();
                        textObject.text = shareData.getText();
                        alipaySendReq(context, instance, api, textObject, share, listener);
                    } else if (MultiMedia.WEIBO == mMultiMedia) {
                        instance.getWBApi(new Callback<IWBAPI>() {
                            @Override
                            public void onSuccess(@NonNull IWBAPI api) {
                                if (!instance.isInstalled(mMultiMedia)) {
                                    // 未安装
                                    ShareAction.this.onFailure(instance, listener, share, -8001, context.getString(R.string.multi_api_app_not_installed));
                                    return;
                                }
                                ShareText shareData = (ShareText) share;
                                if (!shareData.isValid()) {
                                    ShareAction.this.onFailure(instance, listener, share, -8003, context.getString(R.string.multi_api_parameter_error));
                                    return;
                                }
                                TextObject textObject = new TextObject();
                                textObject.text = shareData.getText();
                                textObject.description = share.getDescription();
                                wbSendReq(instance, api, activity, textObject, share, listener);
                            }

                            @Override
                            public void onFailure(int code, @NonNull String msg) {
                                ShareAction.this.onFailure(instance, listener, share, code, msg);
                            }
                        });
                    } else {
                        onFailure(instance, listener, share, -8002, context.getString(R.string.multi_api_feature_not_supported));
                    }
                } else if (share instanceof ShareImage) {
                    if (MultiMedia.WECHAT == mMultiMedia) {
                        IWXAPI api = getWXApi(instance, share, listener);
                        if (api == null) {
                            return;
                        }
                        ShareImage shareData = (ShareImage) share;
                        List<ImageObject> images = shareData.getImages();
                        if (images.isEmpty()) {
                            onFailure(instance, listener, share, -8003, context.getString(R.string.multi_api_parameter_error));
                            return;
                        }
                        ImageObject image = images.get(0);
                        String filePath = Util.getFilePath(context, image, true);
                        if (filePath == null || filePath.isEmpty()) {
                            onFailure(instance, listener, share, -8009, context.getString(R.string.multi_api_failed_to_save_file_locally));
                            return;
                        }
                        if (mCompressListener != null) {
                            List<String> list = new ArrayList<>();
                            list.add(filePath);
                            mCompressListener.onCompress(list, new Callback<>() {
                                @Override
                                public void onSuccess(@NonNull List<String> data) {
                                    if (data.isEmpty()) {
                                        ShareAction.this.onFailure(instance, listener, share, -8013, context.getString(R.string.multi_api_compressed_image_list_has_no_data));
                                        return;
                                    }
                                    String path = data.get(0);
                                    File file = new File(path);
                                    if (!file.exists()) {
                                        ShareAction.this.onFailure(instance, listener, share, -8009, context.getString(R.string.multi_api_failed_to_save_file_locally));
                                        return;
                                    }
                                    WXImageObject wxObj = null;
                                    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N && api.getWXAppSupportAPI() >= 0x27000D00) {
                                        Uri uri = Util.getUri(context, "com.tencent.mm", file, instance.getFileProvider());
                                        if (uri == null) {
                                            ShareAction.this.onFailure(instance, listener, share, -8009, context.getString(R.string.multi_api_failed_to_save_file_locally));
                                            return;
                                        }
                                        wxObj = new WXImageObject();
                                        wxObj.imagePath = uri.toString();
                                    } else {
                                        wxObj = new WXImageObject();
                                        wxObj.imagePath = path;
                                    }
                                    wxObj.entranceMiniProgramUsername = shareData.getMiniProgramUsername();
                                    wxObj.entranceMiniProgramPath = shareData.getMiniProgramPath();
                                    wxSendReq(context, instance, api, wxObj, share, listener);
                                }

                                @Override
                                public void onFailure(int code, @NonNull String msg) {
                                    ShareAction.this.onFailure(instance, listener, share, code, msg);
                                }
                            });
                            return;
                        }
                        File file = new File(filePath);
                        if (!file.exists()) {
                            onFailure(instance, listener, share, -8009, context.getString(R.string.multi_api_failed_to_save_file_locally));
                            return;
                        }
                        WXImageObject wxObj = null;
                        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N && api.getWXAppSupportAPI() >= 0x27000D00) {
                            Uri uri = Util.getUri(context, "com.tencent.mm", file, instance.getFileProvider());
                            if (uri == null) {
                                onFailure(instance, listener, share, -8009, context.getString(R.string.multi_api_failed_to_save_file_locally));
                                return;
                            }
                            wxObj = new WXImageObject();
                            wxObj.imagePath = uri.toString();
                        } else {
                            wxObj = new WXImageObject();
                            wxObj.imagePath = filePath;
                        }
                        wxObj.entranceMiniProgramUsername = shareData.getMiniProgramUsername();
                        wxObj.entranceMiniProgramPath = shareData.getMiniProgramPath();
                        wxSendReq(context, instance, api, wxObj, share, listener);
                    } else if (MultiMedia.QQ == mMultiMedia) {
                        Tencent tencent = getTencent(instance, share, listener);
                        if (tencent == null) {
                            return;
                        }
                        ShareImage shareData = (ShareImage) share;
                        List<ImageObject> images = shareData.getImages();
                        if (images.isEmpty()) {
                            onFailure(instance, listener, share, -8003, context.getString(R.string.multi_api_parameter_error));
                            return;
                        }
                        Bundle params = new Bundle();
                        if (mScene == ShareScene.FRIEND) {
                            ImageObject image = images.get(0);
                            String filePath = Util.getFilePath(context, image, true);
                            if (filePath == null || filePath.isEmpty()) {
                                onFailure(instance, listener, share, -8009, context.getString(R.string.multi_api_failed_to_save_file_locally));
                                return;
                            }
                            if (mCompressListener != null) {
                                List<String> list = new ArrayList<>();
                                list.add(filePath);
                                mCompressListener.onCompress(list, new Callback<>() {
                                    @Override
                                    public void onSuccess(@NonNull List<String> data) {
                                        if (data.isEmpty()) {
                                            ShareAction.this.onFailure(instance, listener, share, -8013, context.getString(R.string.multi_api_compressed_image_list_has_no_data));
                                            return;
                                        }
                                        String path = data.get(0);
                                        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_IMAGE);
                                        params.putString(QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL, path);
                                        qqSendReq(instance, tencent, activity, params, share, listener, 1);
                                    }

                                    @Override
                                    public void onFailure(int code, @NonNull String msg) {
                                        ShareAction.this.onFailure(instance, listener, share, code, msg);
                                    }
                                });
                                return;
                            }
                            params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_IMAGE);
                            params.putString(QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL, filePath);
                            qqSendReq(instance, tencent, activity, params, share, listener, 1);
                        } else if (mScene == ShareScene.CIRCLE) {
                            ArrayList<String> list = new ArrayList<>();
                            List<String> compressList = new ArrayList<>();
                            boolean isCompress = mCompressListener != null;
                            for (ImageObject image : images) {
                                String filePath = Util.getFilePath(context, image, true);
                                if (!TextUtils.isEmpty(filePath)) {
                                    if (isCompress) {
                                        compressList.add(filePath);
                                    } else {
                                        list.add(filePath);
                                    }
                                }
                            }
                            if (mCompressListener != null) {
                                mCompressListener.onCompress(compressList, new Callback<>() {
                                    @Override
                                    public void onSuccess(@NonNull List<String> data) {
                                        if (data.isEmpty()) {
                                            ShareAction.this.onFailure(instance, listener, share, -8013, context.getString(R.string.multi_api_compressed_image_list_has_no_data));
                                            return;
                                        }
                                        ArrayList<String> pathList = new ArrayList<>(data);
                                        if (list.size() > 1) {
                                            params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QzonePublish.PUBLISH_TO_QZONE_TYPE_PUBLISHMOOD);
                                            params.putStringArrayList(QQShare.SHARE_TO_QQ_IMAGE_URL, pathList);
                                            qqSendReq(instance, tencent, activity, params, share, listener, 3);
                                        } else {
                                            params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT);
                                            params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, shareData.getTargetUrl());
                                            params.putStringArrayList(QQShare.SHARE_TO_QQ_IMAGE_URL, pathList);
                                            qqSendReq(instance, tencent, activity, params, share, listener, 2);
                                        }
                                    }

                                    @Override
                                    public void onFailure(int code, @NonNull String msg) {
                                        ShareAction.this.onFailure(instance, listener, share, code, msg);
                                    }
                                });
                                return;
                            }
                            if (list.size() > 1) {
                                params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QzonePublish.PUBLISH_TO_QZONE_TYPE_PUBLISHMOOD);
                                params.putStringArrayList(QQShare.SHARE_TO_QQ_IMAGE_URL, list);
                                qqSendReq(instance, tencent, activity, params, share, listener, 3);
                            } else {
                                params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT);
                                params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, shareData.getTargetUrl());
                                params.putStringArrayList(QQShare.SHARE_TO_QQ_IMAGE_URL, list);
                                qqSendReq(instance, tencent, activity, params, share, listener, 2);
                            }
                        } else {
                            onFailure(instance, listener, null, -9500, context.getString(R.string.multi_api_sharing_type_error));
                        }
                    } else if (MultiMedia.ALIPAY == mMultiMedia) {
                        IAPApi api = getAPApi(instance, share, listener);
                        if (api == null) {
                            return;
                        }
                        ShareImage shareData = (ShareImage) share;
                        List<ImageObject> images = shareData.getImages();
                        if (images.isEmpty()) {
                            onFailure(instance, listener, share, -8003, context.getString(R.string.multi_api_parameter_error));
                            return;
                        }
                        ImageObject image = images.get(0);
                        String filePath = Util.getFilePath(context, image, true);
                        if (filePath == null || filePath.isEmpty()) {
                            onFailure(instance, listener, share, -8009, context.getString(R.string.multi_api_failed_to_save_file_locally));
                            return;
                        }
                        if (mCompressListener != null) {
                            List<String> list = new ArrayList<>();
                            list.add(filePath);
                            mCompressListener.onCompress(list, new Callback<>() {
                                @Override
                                public void onSuccess(@NonNull List<String> data) {
                                    if (data.isEmpty()) {
                                        ShareAction.this.onFailure(instance, listener, share, -8013, context.getString(R.string.multi_api_compressed_image_list_has_no_data));
                                        return;
                                    }
                                    String path = data.get(0);
                                    File file = new File(path);
                                    if (!file.exists()) {
                                        ShareAction.this.onFailure(instance, listener, share, -8009, context.getString(R.string.multi_api_failed_to_save_file_locally));
                                        return;
                                    }
                                    APImageObject imageObject = null;
                                    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                                        Uri uri = Util.getUri(context, "com.eg.android.AlipayGphone", file, instance.getFileProvider());
                                        if (uri == null) {
                                            ShareAction.this.onFailure(instance, listener, share, -8009, context.getString(R.string.multi_api_failed_to_save_file_locally));
                                            return;
                                        }
                                        imageObject = new APImageObject();
                                        imageObject.imagePath = uri.toString();
                                    } else {
                                        imageObject = new APImageObject();
                                        imageObject.imagePath = path;
                                    }
                                    alipaySendReq(context, instance, api, imageObject, share, listener);
                                }

                                @Override
                                public void onFailure(int code, @NonNull String msg) {
                                    ShareAction.this.onFailure(instance, listener, share, code, msg);
                                }
                            });
                            return;
                        }
                        File file = new File(filePath);
                        if (!file.exists()) {
                            onFailure(instance, listener, share, -8009, context.getString(R.string.multi_api_failed_to_save_file_locally));
                            return;
                        }
                        APImageObject imageObject = null;
                        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                            Uri uri = Util.getUri(context, "com.eg.android.AlipayGphone", file, instance.getFileProvider());
                            if (uri == null) {
                                onFailure(instance, listener, share, -8009, context.getString(R.string.multi_api_failed_to_save_file_locally));
                                return;
                            }
                            imageObject = new APImageObject();
                            imageObject.imagePath = uri.toString();
                        } else {
                            imageObject = new APImageObject();
                            imageObject.imagePath = filePath;
                        }
                        alipaySendReq(context, instance, api, imageObject, share, listener);
                    } else if (MultiMedia.WEIBO == mMultiMedia) {
                        instance.getWBApi(new Callback<IWBAPI>() {
                            @Override
                            public void onSuccess(@NonNull IWBAPI api) {
                                if (!instance.isInstalled(mMultiMedia)) {
                                    // 未安装
                                    ShareAction.this.onFailure(instance, listener, share, -8001, context.getString(R.string.multi_api_app_not_installed));
                                    return;
                                }
                                ShareImage shareData = (ShareImage) share;
                                List<ImageObject> images = shareData.getImages();
                                if (images.isEmpty()) {
                                    ShareAction.this.onFailure(instance, listener, share, -8003, context.getString(R.string.multi_api_parameter_error));
                                    return;
                                }
                                String fileProvider = instance.getFileProvider();
                                if (fileProvider == null || fileProvider.isEmpty()) {
                                    ShareAction.this.onFailure(instance, listener, null, -8011, context.getString(R.string.multi_api_no_read_permission));
                                    return;
                                }
                                if (images.size() == 1) {
                                    // 单图
                                    ImageObject image = images.get(0);
                                    String filePath = Util.getFilePath(context, image, true);
                                    if (filePath == null || filePath.isEmpty()) {
                                        ShareAction.this.onFailure(instance, listener, share, -8009, context.getString(R.string.multi_api_failed_to_save_file_locally));
                                        return;
                                    }
                                    if (mCompressListener != null) {
                                        List<String> list = new ArrayList<>();
                                        list.add(filePath);
                                        mCompressListener.onCompress(list, new Callback<>() {
                                            @Override
                                            public void onSuccess(@NonNull List<String> data) {
                                                if (data.isEmpty()) {
                                                    ShareAction.this.onFailure(instance, listener, share, -8013, context.getString(R.string.multi_api_compressed_image_list_has_no_data));
                                                    return;
                                                }
                                                String path = data.get(0);
                                                Uri uri = Util.getUri(context, path, fileProvider);
                                                com.sina.weibo.sdk.api.ImageObject imageObject = new com.sina.weibo.sdk.api.ImageObject();
                                                imageObject.imagePath = uri.toString();
                                                wbSendReq(instance, api, activity, imageObject, share, listener);
                                            }

                                            @Override
                                            public void onFailure(int code, @NonNull String msg) {
                                                ShareAction.this.onFailure(instance, listener, share, code, msg);
                                            }
                                        });
                                        return;
                                    }
                                    Uri uri = Util.getUri(context, filePath, fileProvider);
                                    com.sina.weibo.sdk.api.ImageObject imageObject = new com.sina.weibo.sdk.api.ImageObject();
                                    imageObject.imagePath = uri.toString();
                                    wbSendReq(instance, api, activity, imageObject, share, listener);
                                } else {
                                    // 多图
                                    MultiImageObject multiImageObject = new MultiImageObject();
                                    ArrayList<Uri> list = new ArrayList<>();
                                    List<String> compressList = new ArrayList<>();
                                    boolean isCompress = mCompressListener != null;
                                    for (ImageObject image : images) {
                                        String filePath = Util.getFilePath(context, image, true);
                                        if (!TextUtils.isEmpty(filePath)) {
                                            if (isCompress) {
                                                compressList.add(filePath);
                                            } else {
                                                Uri uri = Util.getUri(context, filePath, fileProvider);
                                                if (uri != null) {
                                                    list.add(uri);
                                                }
                                            }
                                        }
                                    }
                                    if (mCompressListener != null) {
                                        mCompressListener.onCompress(compressList, new Callback<>() {
                                            @Override
                                            public void onSuccess(@NonNull List<String> data) {
                                                if (data.isEmpty()) {
                                                    ShareAction.this.onFailure(instance, listener, share, -8013, context.getString(R.string.multi_api_compressed_image_list_has_no_data));
                                                    return;
                                                }
                                                ArrayList<Uri> uriList = new ArrayList<>();
                                                for (String path : data) {
                                                    Uri uri = Util.getUri(context, path, fileProvider);
                                                    if (uri != null) {
                                                        uriList.add(uri);
                                                    }
                                                }
                                                multiImageObject.imageList = uriList;
                                                wbSendReq(instance, api, activity, multiImageObject, share, listener);
                                            }

                                            @Override
                                            public void onFailure(int code, @NonNull String msg) {
                                                ShareAction.this.onFailure(instance, listener, share, code, msg);
                                            }
                                        });
                                        return;
                                    }
                                    multiImageObject.imageList = list;
                                    wbSendReq(instance, api, activity, multiImageObject, share, listener);
                                }
                            }

                            @Override
                            public void onFailure(int code, @NonNull String msg) {
                                ShareAction.this.onFailure(instance, listener, share, code, msg);
                            }
                        });
                    } else {
                        onFailure(instance, listener, share, -8002, context.getString(R.string.multi_api_feature_not_supported));
                    }
                } else if (share instanceof ShareMini) {
                    if (MultiMedia.WECHAT == mMultiMedia) {
                        IWXAPI api = getWXApi(instance, share, listener);
                        if (api == null) {
                            return;
                        }
                        ShareMini shareData = (ShareMini) share;
                        if (!shareData.isValid()) {
                            onFailure(instance, listener, share, -8003, context.getString(R.string.multi_api_parameter_error));
                            return;
                        }
                        WXMiniProgramObject wxObj = new WXMiniProgramObject();
                        wxObj.miniprogramType = WXMiniProgramObject.MINIPTOGRAM_TYPE_RELEASE;
                        wxObj.webpageUrl = shareData.getTargetUrl();
                        wxObj.userName = shareData.getUserName();
                        wxObj.path = shareData.getPath();
                        wxSendReq(context, instance, api, wxObj, share, listener);
                    } else if (MultiMedia.QQ == mMultiMedia) {
                        Tencent tencent = getTencent(instance, share, listener);
                        if (tencent == null) {
                            return;
                        }
                        ShareMini shareData = (ShareMini) share;
                        if (!shareData.isValid()) {
                            onFailure(instance, listener, share, -8003, context.getString(R.string.multi_api_parameter_error));
                            return;
                        }
                        ImageObject image = shareData.getThumbImage();
                        if (image == null) {
                            onFailure(instance, listener, share, -8003, context.getString(R.string.multi_api_parameter_error));
                            return;
                        }
                        Bundle params = new Bundle();
                        if (mScene == ShareScene.FRIEND) {
                            String filePath = Util.getFilePath(context, image, false);
                            params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_MINI_PROGRAM);
                            params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, filePath);
                            params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, shareData.getTargetUrl());
                            params.putInt(QQShare.SHARE_TO_QQ_MINI_PROGRAM_TYPE, 3);
                            params.putString(QQShare.SHARE_TO_QQ_MINI_PROGRAM_APPID, shareData.getUserName());
                            params.putString(QQShare.SHARE_TO_QQ_MINI_PROGRAM_PATH, shareData.getPath());
                            qqSendReq(instance, tencent, activity, params, share, listener, 1);
                        } else if (mScene == ShareScene.CIRCLE) {
                            ArrayList<String> list = new ArrayList<>();
                            String filePath = Util.getFilePath(context, image, false);
                            if (!TextUtils.isEmpty(filePath)) {
                                list.add(filePath);
                            }
                            params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_MINI_PROGRAM);
                            params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, shareData.getTargetUrl());
                            params.putStringArrayList(QQShare.SHARE_TO_QQ_IMAGE_URL, list);
                            params.putInt(QQShare.SHARE_TO_QQ_MINI_PROGRAM_TYPE, 3);
                            params.putString(QQShare.SHARE_TO_QQ_MINI_PROGRAM_APPID, shareData.getUserName());
                            params.putString(QQShare.SHARE_TO_QQ_MINI_PROGRAM_PATH, shareData.getPath());
                            qqSendReq(instance, tencent, activity, params, share, listener, 2);
                        } else {
                            onFailure(instance, listener, null, -9500, context.getString(R.string.multi_api_sharing_type_error));
                        }
                    } else {
                        onFailure(instance, listener, share, -8002, context.getString(R.string.multi_api_feature_not_supported));
                    }
                } else if (share instanceof ShareMusic) {
                    if (MultiMedia.WECHAT == mMultiMedia) {
                        IWXAPI api = getWXApi(instance, share, listener);
                        if (api == null) {
                            return;
                        }
                        ShareMusic shareData = (ShareMusic) share;
                        if (!shareData.isValid()) {
                            onFailure(instance, listener, share, -8003, context.getString(R.string.multi_api_parameter_error));
                            return;
                        }
                        WXMusicVideoObject wxObj = new WXMusicVideoObject();
                        wxObj.musicUrl = shareData.getTargetUrl();
                        wxObj.musicDataUrl = shareData.getMusicUrl();
                        wxObj.singerName = shareData.getSingerName();
                        wxObj.duration = shareData.getDuration();
                        wxObj.songLyric = shareData.getSongLyric();
                        List<ImageObject> images = shareData.getThumbImages();
                        if (!images.isEmpty()) {
                            ImageObject image = images.get(0);
                            wxObj.hdAlbumThumbFilePath = Util.getFilePath(context, image, true);
                        }
                        wxObj.hdAlbumThumbFileHash = shareData.getThumbFileHash();
                        wxObj.albumName = shareData.getAlbumName();
                        wxObj.musicGenre = shareData.getMusicGenre();
                        wxObj.issueDate = shareData.getIssueDate();
                        wxObj.identification = shareData.getIdentification();
                        wxSendReq(context, instance, api, wxObj, share, listener);
                    } else if (MultiMedia.QQ == mMultiMedia) {
                        Tencent tencent = getTencent(instance, share, listener);
                        if (tencent == null) {
                            return;
                        }
                        ShareMusic shareData = (ShareMusic) share;
                        if (!shareData.isValid()) {
                            onFailure(instance, listener, share, -8003, context.getString(R.string.multi_api_parameter_error));
                            return;
                        }
                        List<ImageObject> images = shareData.getThumbImages();
                        if (images.isEmpty()) {
                            onFailure(instance, listener, share, -8003, context.getString(R.string.multi_api_parameter_error));
                            return;
                        }
                        if (mScene == ShareScene.FRIEND) {
                            Bundle params = new Bundle();
                            ImageObject image = images.get(0);
                            String filePath = Util.getFilePath(context, image, false);
                            params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_AUDIO);
                            params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, filePath);
                            params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, shareData.getTargetUrl());
                            params.putString(QQShare.SHARE_TO_QQ_AUDIO_URL, shareData.getMusicUrl());
                            qqSendReq(instance, tencent, activity, params, share, listener, 1);
                        } else {
                            onFailure(instance, listener, null, -9500, context.getString(R.string.multi_api_sharing_type_error));
                        }
                    } else {
                        onFailure(instance, listener, share, -8002, context.getString(R.string.multi_api_feature_not_supported));
                    }
                } else if (share instanceof ShareVideo) {
                    if (MultiMedia.WECHAT == mMultiMedia) {
                        IWXAPI api = getWXApi(instance, share, listener);
                        if (api == null) {
                            return;
                        }
                        ShareVideo shareData = (ShareVideo) share;
                        if (!shareData.isValid()) {
                            onFailure(instance, listener, share, -8003, context.getString(R.string.multi_api_parameter_error));
                            return;
                        }
                        String videoPath = shareData.getVideoPath();
                        if (!videoPath.startsWith("http")) {
                            onFailure(instance, listener, share, -8003, context.getString(R.string.multi_api_only_supports_network_addresses));
                            return;
                        }
                        WXVideoObject wxObj = new WXVideoObject();
                        wxObj.videoUrl = videoPath;
                        wxObj.videoLowBandUrl = shareData.getVideoLowBandUrl();
                        wxSendReq(context, instance, api, wxObj, share, listener);
                    } else if (MultiMedia.QQ == mMultiMedia) {
                        Tencent tencent = getTencent(instance, share, listener);
                        if (tencent == null) {
                            return;
                        }
                        ShareVideo shareData = (ShareVideo) share;
                        if (!shareData.isValid()) {
                            onFailure(instance, listener, share, -8003, context.getString(R.string.multi_api_parameter_error));
                            return;
                        }
                        if (mScene == ShareScene.CIRCLE) {
                            String videoPath = shareData.getVideoPath();
                            if (videoPath.startsWith("http")) {
                                onFailure(instance, listener, share, -8003, context.getString(R.string.multi_api_only_supports_local_file));
                                return;
                            }
                            Bundle params = new Bundle();
                            params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QzonePublish.PUBLISH_TO_QZONE_TYPE_PUBLISHVIDEO);
                            params.putString(QzonePublish.PUBLISH_TO_QZONE_VIDEO_PATH, videoPath);
                            qqSendReq(instance, tencent, activity, params, share, listener, 3);
                        } else {
                            onFailure(instance, listener, null, -9500, context.getString(R.string.multi_api_only_supports_qq_zone));
                        }
                    } else if (MultiMedia.WEIBO == mMultiMedia) {
                        instance.getWBApi(new Callback<IWBAPI>() {
                            @Override
                            public void onSuccess(@NonNull IWBAPI api) {
                                if (!instance.isInstalled(mMultiMedia)) {
                                    // 未安装
                                    ShareAction.this.onFailure(instance, listener, share, -8001, context.getString(R.string.multi_api_app_not_installed));
                                    return;
                                }
                                ShareVideo shareData = (ShareVideo) share;
                                if (!shareData.isValid()) {
                                    ShareAction.this.onFailure(instance, listener, share, -8003, context.getString(R.string.multi_api_parameter_error));
                                    return;
                                }
                                String videoPath = shareData.getVideoPath();
                                if (videoPath.startsWith("http")) {
                                    ShareAction.this.onFailure(instance, listener, share, -8003, context.getString(R.string.multi_api_only_supports_local_file));
                                    return;
                                }
                                String fileProvider = instance.getFileProvider();
                                if (fileProvider == null || fileProvider.isEmpty()) {
                                    ShareAction.this.onFailure(instance, listener, null, -8011, context.getString(R.string.multi_api_no_read_permission));
                                    return;
                                }
                                Uri uri = Util.getUri(context, videoPath, fileProvider);
                                if (uri != null) {
                                    VideoSourceObject videoObject = new VideoSourceObject();
                                    videoObject.videoPath = uri;
                                    wbSendReq(instance, api, activity, videoObject, share, listener);
                                } else {
                                    ShareAction.this.onFailure(instance, listener, share, -8009, context.getString(R.string.multi_api_failed_to_save_file_locally));
                                }
                            }

                            @Override
                            public void onFailure(int code, @NonNull String msg) {
                                ShareAction.this.onFailure(instance, listener, share, code, msg);
                            }
                        });
                    } else {
                        onFailure(instance, listener, share, -8002, context.getString(R.string.multi_api_feature_not_supported));
                    }
                } else if (share instanceof ShareWeb) {
                    if (MultiMedia.WECHAT == mMultiMedia) {
                        IWXAPI api = getWXApi(instance, share, listener);
                        if (api == null) {
                            return;
                        }
                        ShareWeb shareData = (ShareWeb) share;
                        if (!shareData.isValid()) {
                            onFailure(instance, listener, share, -8003, context.getString(R.string.multi_api_parameter_error));
                            return;
                        }
                        WXWebpageObject wxObj = new WXWebpageObject();
                        wxObj.webpageUrl = shareData.getTargetUrl();
                        wxSendReq(context, instance, api, wxObj, share, listener);
                    } else if (MultiMedia.QQ == mMultiMedia) {
                        Tencent tencent = getTencent(instance, share, listener);
                        if (tencent == null) {
                            return;
                        }
                        ShareWeb shareData = (ShareWeb) share;
                        if (!shareData.isValid()) {
                            onFailure(instance, listener, share, -8003, context.getString(R.string.multi_api_parameter_error));
                            return;
                        }
                        Bundle params = new Bundle();
                        if (mScene == ShareScene.FRIEND) {
                            ImageObject image = shareData.getThumbImage();
                            String filePath = Util.getFilePath(context, image, false);
                            params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
                            params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, filePath);
                            params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, shareData.getTargetUrl());
                            qqSendReq(instance, tencent, activity, params, share, listener, 1);
                        } else if (mScene == ShareScene.CIRCLE) {
                            ArrayList<String> list = new ArrayList<>();
                            String filePath = Util.getFilePath(context, shareData.getThumbImage(), false);
                            if (!TextUtils.isEmpty(filePath)) {
                                list.add(filePath);
                            }
                            params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT);
                            params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, shareData.getTargetUrl());
                            params.putStringArrayList(QQShare.SHARE_TO_QQ_IMAGE_URL, list);
                            qqSendReq(instance, tencent, activity, params, share, listener, 2);
                        } else {
                            onFailure(instance, listener, null, -9500, context.getString(R.string.multi_api_sharing_type_error));
                        }
                    } else if (MultiMedia.ALIPAY == mMultiMedia) {
                        IAPApi api = getAPApi(instance, share, listener);
                        if (api == null) {
                            return;
                        }
                        ShareWeb shareData = (ShareWeb) share;
                        if (!shareData.isValid()) {
                            onFailure(instance, listener, share, -8003, context.getString(R.string.multi_api_parameter_error));
                            return;
                        }
                        APWebPageObject webPageObject = new APWebPageObject();
                        webPageObject.webpageUrl = share.getTargetUrl();
                        alipaySendReq(context, instance, api, webPageObject, share, listener);
                    } else if (MultiMedia.WEIBO == mMultiMedia) {
                        instance.getWBApi(new Callback<IWBAPI>() {
                            @Override
                            public void onSuccess(@NonNull IWBAPI api) {
                                if (!instance.isInstalled(mMultiMedia)) {
                                    // 未安装
                                    ShareAction.this.onFailure(instance, listener, share, -8001, context.getString(R.string.multi_api_app_not_installed));
                                    return;
                                }
                                ShareWeb shareData = (ShareWeb) share;
                                if (!shareData.isValid()) {
                                    ShareAction.this.onFailure(instance, listener, share, -8003, context.getString(R.string.multi_api_parameter_error));
                                    return;
                                }
                                WebpageObject webObject = new WebpageObject();
                                webObject.title = shareData.getTitle();
                                webObject.description = shareData.getDescription();
                                webObject.actionUrl = shareData.getTargetUrl();
                                webObject.defaultText = shareData.getTitle();
                                webObject.thumbData = shareData.getThumbData(context);
                                wbSendReq(instance, api, activity, webObject, share, listener);
                            }

                            @Override
                            public void onFailure(int code, @NonNull String msg) {
                                ShareAction.this.onFailure(instance, listener, share, code, msg);
                            }
                        });
                    } else {
                        onFailure(instance, listener, share, -8002, context.getString(R.string.multi_api_feature_not_supported));
                    }
                } else {
                    onFailure(instance, listener, share, -8003, context.getString(R.string.multi_api_parameter_error));
                }
            } catch (Throwable t) {
                onFailure(instance, listener, share, -8003, t.getMessage());
            }
        });
        if (!status) {
            onFailure(instance, listener, share, -8007, context.getString(R.string.multi_api_request_initiation_failed));
        }
    }

    /**
     * 完成回调监听
     *
     * @param listener
     * @param map
     */
    private void onComplete(@NonNull MultiApi instance, OnActionListener listener, @NonNull Map<String, String> map) {
        if (listener == null) {
            return;
        }
        MultiMedia multiMedia = mMultiMedia;
        instance.post(() -> listener.onComplete(multiMedia, map));
    }

    /**
     * 取消回调监听
     *
     * @param listener
     */
    private void onCancel(@NonNull MultiApi instance, OnActionListener listener) {
        if (listener == null) {
            return;
        }
        MultiMedia multiMedia = mMultiMedia;
        instance.post(() -> listener.onCancel(multiMedia));
    }

    /**
     * 失败回调监听
     *
     * @param listener
     * @param share
     * @param code
     */
    private void onFailure(@NonNull MultiApi instance, OnActionListener listener, Share share, int code, String msg) {
        if (share != null) {
            share.release();
        }
        if (listener == null) {
            return;
        }
        MultiMedia multiMedia = mMultiMedia;
        instance.post(() -> listener.onFailure(multiMedia, code, msg));
    }

    /**
     * 获取微信对象
     *
     * @param listener
     */
    private IWXAPI getWXApi(@NonNull MultiApi instance, Share share, OnActionListener listener) {
        IWXAPI api = instance.getWXApi();
        if (api == null) {
            onFailure(instance, listener, share, -8005, instance.getContext().getString(R.string.multi_api_not_initialized_successfully));
            return null;
        } else {
            if (!instance.isInstalled(mMultiMedia)) {
                // 未安装
                onFailure(instance, listener, share, -8001, instance.getContext().getString(R.string.multi_api_app_not_installed));
                return null;
            }
        }
        return api;
    }

    /**
     * 微信发起请求
     *
     * @param api
     * @param mediaObject
     * @param share
     * @param listener
     */
    private void wxSendReq(@NonNull Context context, @NonNull MultiApi instance, @NonNull IWXAPI api, @NonNull WXMediaMessage.IMediaObject mediaObject, @NonNull Share share, OnActionListener listener) {
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = mediaObject;
        msg.title = share.getTitle();
        msg.description = share.getDescription();
        msg.thumbData = share.getThumbData(instance.getContext());
        share.release();
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.message = msg;
        String state = Util.buildState();
        req.transaction = state;
        OnActionCallback callback = new OnActionCallback(3, listener);
        callback.state = state;
        instance.setMultiMedia(mMultiMedia)
                .setActionCallback(callback);
        if (mScene == ShareScene.FRIEND) {
            req.scene = SendMessageToWX.Req.WXSceneSession;
        } else if (mScene == ShareScene.CIRCLE) {
            req.scene = SendMessageToWX.Req.WXSceneTimeline;
        } else if (mScene == ShareScene.FAVORITE) {
            req.scene = SendMessageToWX.Req.WXSceneFavorite;
        } else {
            instance.release();
            onFailure(instance, listener, null, -9500, instance.getContext().getString(R.string.multi_api_sharing_type_error));
            return;
        }
        try {
            if (!api.sendReq(req)) {
                instance.release();
                onFailure(instance, listener, null, -8004, context.getString(R.string.multi_api_filter_logs_check_reason, "MicroMsg.SDK"));
            }
        } catch (Throwable t) {
            instance.release();
            onFailure(instance, listener, null, -8004, t.getMessage());
        }
    }

    /**
     * 获取 QQ 授权登录和分享对象
     *
     * @param listener
     */
    private Tencent getTencent(@NonNull MultiApi instance, Share share, OnActionListener listener) {
        Tencent tencent = instance.getTencent();
        if (tencent == null) {
            onFailure(instance, listener, share, -8005, instance.getContext().getString(R.string.multi_api_not_initialized_successfully));
            return null;
        } else {
            if (!instance.isInstalled(mMultiMedia)) {
                // 未安装
                onFailure(instance, listener, share, -8001, instance.getContext().getString(R.string.multi_api_app_not_installed));
                return null;
            }
        }
        return tencent;
    }

    /**
     * QQ发起请求
     *
     * @param tencent
     * @param share
     * @param listener
     * @param type     类型：1 QQ，2 qq空间，3 qq空间多图、视频
     */
    private void qqSendReq(@NonNull MultiApi instance, @NonNull Tencent tencent, Activity activity, @NonNull Bundle params, Share share, OnActionListener listener, int type) {
        params.putString(QQShare.SHARE_TO_QQ_TITLE, share.getTitle());
        params.putString(QQShare.SHARE_TO_QQ_SUMMARY, share.getDescription());
        share.release();
        Context context = instance.getContext();
        IUiListener uiListener = new IUiListener() {

            @Override
            public void onComplete(Object o) {
                instance.release();
                Map<String, String> map = new HashMap<>();
                ShareAction.this.onComplete(instance, listener, map);
            }

            @Override
            public void onError(UiError uiError) {
                instance.release();
                int errorCode = -9900;
                String errorMsg = "";
                if (uiError != null) {
                    errorCode = uiError.errorCode;
                    errorMsg = uiError.errorMessage;
                }
                if (TextUtils.isEmpty(errorMsg)) {
                    errorMsg = context.getString(R.string.multi_api_unknown_error);
                }
                if (errorCode == Constants.ERROR_NO_AUTHORITY) {
                    errorCode = -8011;
                    errorMsg = context.getString(R.string.multi_api_no_read_permission);
                    onFailure(instance, listener, null, errorCode, errorMsg);
                    return;
                }
                onFailure(instance, listener, null, errorCode, errorMsg);
            }

            @Override
            public void onCancel() {
                instance.release();
                ShareAction.this.onCancel(instance, listener);
            }

            @Override
            public void onWarning(int i) {

            }
        };
        OnActionCallback callback = new OnActionCallback(3, listener);
        instance.setMultiMedia(mMultiMedia)
                .setActionCallback(callback);
        try {
            if (type == 1) {
                tencent.shareToQQ(activity, params, uiListener);
            } else if (type == 2) {
                tencent.shareToQzone(activity, params, uiListener);
            } else if (type == 3) {
                if (Tencent.isSupportPushToQZone(context)) {
                    tencent.publishToQzone(activity, params, uiListener);
                } else {
                    instance.release();
                    onFailure(instance, listener, share, -8002, context.getString(R.string.multi_api_feature_not_supported));
                }
            } else {
                instance.release();
                onFailure(instance, listener, null, -9500, context.getString(R.string.multi_api_sharing_type_error));
            }
        } catch (Throwable t) {
            instance.release();
            onFailure(instance, listener, null, -8004, t.getMessage());
        }
    }

    /**
     * 获取支付宝授权登录和分享对象
     *
     * @param listener
     */
    private IAPApi getAPApi(@NonNull MultiApi instance, Share share, OnActionListener listener) {
        IAPApi apApi = instance.getAPApi();
        if (apApi == null) {
            onFailure(instance, listener, share, -8005, instance.getContext().getString(R.string.multi_api_not_initialized_successfully));
            return null;
        } else {
            if (!instance.isInstalled(mMultiMedia)) {
                // 未安装
                onFailure(instance, listener, share, -8001, instance.getContext().getString(R.string.multi_api_app_not_installed));
                return null;
            }
        }
        return apApi;
    }

    /**
     * 支付宝发起请求
     *
     * @param apApi
     * @param share
     * @param listener
     */
    private void alipaySendReq(@NonNull Context context, @NonNull MultiApi instance, @NonNull IAPApi apApi, @NonNull APMediaMessage.IMediaObject mediaObject, Share share, OnActionListener listener) {
        APMediaMessage mediaMessage = new APMediaMessage();
        mediaMessage.title = share.getTitle();
        mediaMessage.description = share.getDescription();
        mediaMessage.thumbData = share.getThumbData(instance.getContext());
        mediaMessage.mediaObject = mediaObject;
        SendMessageToZFB.Req req = new SendMessageToZFB.Req();
        req.message = mediaMessage;
        share.release();
        OnActionCallback callback = new OnActionCallback(3, listener);
        instance.setMultiMedia(mMultiMedia)
                .setActionCallback(callback);
        share.release();
        try {
            if (!apApi.sendReq(req)) {
                instance.release();
                onFailure(instance, listener, null, -8004, context.getString(R.string.multi_api_filter_logs_check_reason, "APSDK"));
            }
        } catch (Throwable t) {
            instance.release();
            onFailure(instance, listener, null, -8004, t.getMessage());
        }
    }

    /**
     * 微博发起请求
     *
     * @param wbApi
     * @param share
     * @param listener
     */
    private void wbSendReq(@NonNull MultiApi instance, @NonNull IWBAPI wbApi, Activity activity, @NonNull MediaObject mediaObject, Share share, OnActionListener listener) {
        WeiboMultiMessage message = new WeiboMultiMessage();
        String title = share.getTitle();
        if (mediaObject instanceof TextObject) {
            message.textObject = (TextObject) mediaObject;
        } else if (mediaObject instanceof WebpageObject) {
        } else if (!TextUtils.isEmpty(title)) {
            TextObject textObject = new TextObject();
            textObject.text = share.getTitle();
            textObject.description = share.getDescription();
            message.textObject = textObject;
        }
        if (mediaObject instanceof com.sina.weibo.sdk.api.ImageObject) {
            message.imageObject = (com.sina.weibo.sdk.api.ImageObject) mediaObject;
        } else if (mediaObject instanceof MultiImageObject) {
            message.multiImageObject = (MultiImageObject) mediaObject;
        } else if (mediaObject instanceof VideoSourceObject) {
            message.videoSourceObject = (VideoSourceObject) mediaObject;
        } else if (mediaObject instanceof SuperGroupObject) {
            message.superGroupObject = (SuperGroupObject) mediaObject;
        } else if (!(mediaObject instanceof TextObject)) {
            message.mediaObject = mediaObject;
        }
        share.release();
        OnActionCallback callback = new OnActionCallback(3, listener);
        instance.setMultiMedia(mMultiMedia)
                .setActionCallback(callback);
        try {
            wbApi.shareMessage(activity, message, false);
        } catch (Throwable t) {
            instance.release();
            onFailure(instance, listener, null, -8004, t.getMessage());
        }
    }
}
